package com.lysj.website.website.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.lysj.website.common.spring.base.BaseController;
import com.lysj.website.common.web.ParamUtil;
import com.lysj.website.common.web.Resp;
import com.lysj.website.website.RandomConstant;
import com.lysj.website.website.domain.TodayBrowseAmount;
import com.lysj.website.website.service.TodayBrowseAmountService;
import com.lysj.website.website.service.WebsiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 16:33 2019/10/29
 * @ Description:
 **/
@Slf4j
@RestController
@RequestMapping("/website/today_browse_amount")
public class TodayBrowseAmountController extends BaseController<TodayBrowseAmount> {


    @Resource
    private TodayBrowseAmountService todayBrowseAmountService;


    @Override
    public TodayBrowseAmountService getService() {
        return todayBrowseAmountService;
    }

    @GetMapping("/query")
    public Resp query() {
        log.info("查询今日浏览信息 request ip:{}--------->>>>>>>>>>>" + request.getRemoteAddr());
        Integer initRandom = RandomUtil.randomInt(RandomConstant.GROWTH_START,RandomConstant.GROWTH_END);
        //查询今日总的浏览数量
        TodayBrowseAmount todayBrowseAmount = todayBrowseAmountService.getRepository().findByCreateTimeBetween(DateUtil.beginOfMonth(new Date()).toJdkDate(), DateUtil.endOfMonth(new Date()).toJdkDate());
        if (ParamUtil.isBlank(todayBrowseAmount)) {
            todayBrowseAmount = new TodayBrowseAmount();
            todayBrowseAmount.setAmount(initRandom);
            todayBrowseAmount.setCreateTime(new Date());
            todayBrowseAmountService.save(todayBrowseAmount);
        }
        return new Resp().success(todayBrowseAmount.getAmount());
    }
}
